package org.skydiveapphd;

import org.skydiveapphd.ui.FragmentHelper;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.repository.RigRepository;
import org.skydiveapplibrary.ui.GearListAdapter;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class GearListActivity extends BaseExpandableListActivity
{
	
    @Override 
    public void onCreate(Bundle savedInstanceState)
    { 
    	super.onCreate(savedInstanceState);
    	
    	getExpandableListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
    	setHeaderText(R.string.gear_list_label);
    	setBottomButtonVisibility(View.GONE);
    }
    
    @Override
    public void onResume()
    {
    	super.onResume();
    	
    	// always re-load data
    	loadData();
    }
    
    @Override
    public boolean onChildClick(android.widget.ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
    {
    	// start edit
    	// Create new fragment and transaction
    	Bundle args = new Bundle();
    	args.putInt(Constants.EXTRA_RIG_ID, (int)id);
    	args.putBoolean(Constants.EXTRA_FROM_GEAR_TAB, true);
		
    	FragmentHelper.getInstance().openFragment(RigEditActivity.newInstance(args), android.R.id.tabhost, 
				Constants.TAG_RIG_EDIT, getParent().getFragmentManager());
		
		return true;
    }
    
    @Override
    protected void onTopButtonClick()
    {
    	// Create new fragment and transaction
    	Bundle args = new Bundle();
    	args.putBoolean(Constants.EXTRA_FROM_GEAR_TAB, true);

    	FragmentHelper.getInstance().openFragment(RigEditActivity.newInstance(args), android.R.id.tabhost, 
				Constants.TAG_RIG_EDIT, getParent().getFragmentManager());
    }
    
    public void loadData()
    {
    	// get repos
    	RigRepository rigRepository = new RigRepository(this);
    	GearListAdapter adapter = new GearListAdapter(
    			this,
    			rigRepository.getRigs(),
    			rigRepository.getArchivedRigs());
    	
    	// set adapter
		setListAdapter(adapter);
		
		// expand groups
		for (int group = 0; group < adapter.getGroupCount(); group++)
		{
			getExpandableListView().expandGroup(group);
		}
    }
} 